import { REQUEST_LOGIN, RECEIVE_LOGIN, 
    LOGOUT,
    NEWPROFILE,
    PAGE_CHANGE,
    REGISTER_INPROCESS,
    ACTIVATE_INPROCESS
  } from '../constants/ActionTypes'
const initialState =
{
    username: '',
    first_name: '',
    last_name: '',
    isLogin: false,
    profileFetched: false,
    isLoggingIn: false,
    isUpdate: false,
    error: null,
    key: '',
    shouldActivate: false,
    activateComplete: false
}
export default function userControl(state = initialState, action){
    switch(action.type){
        case REQUEST_LOGIN:
            return Object.assign({}, state, {
                isLoggingIn: true
            })
        case RECEIVE_LOGIN:
            if(action.error == null)
                return Object.assign({}, state, {
                    username: action.username,
                    isLogin: true,
                    isLoggingIn: false,
                    error: null,
                    key: action.key
                })
            return Object.assign({}, state, {
                username: '',
                isLogin: false,
                isLoggingIn: false,
                error: JSON.stringify(action.error)
            })
        case LOGOUT:
            if(action.error == null)
                return Object.assign({}, state, {
                    username: '',
                    isLogin: false,
                    error: null,
                    key: ''
                })
            else
                return Object.assign({}, state, {error: JSON.stringify(action.error)})
        case NEWPROFILE:
            if(action.error == null)
                return Object.assign({}, state, action.newProfile)
            return Object.assign({}, state, {error: JSON.stringify(action.error)})
        case PAGE_CHANGE:
            return Object.assign({}, state, { isUpdate: false, error: null })
        case REGISTER_INPROCESS:
            if (action.error == null)
                return Object.assign({}, state, { shouldActivate: true, error: null })
            return Object.assign({}, state, { error: JSON.stringify(action.error) })
        case ACTIVATE_INPROCESS:
            if (action.error == null)
                return Object.assign({}, state, { activateComplete: true, error: null })
            return Object.assign({}, state, { error: JSON.stringify(action.error) })
        default:            
            return Object.assign({}, state, {error: null})
    }
}
